//
//  FKGiftBagListVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/19.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKGiftBagListVC.h"
#import "FKBusinessGiftBagCell.h"
#import "FKCommodityDetailVC.h"

@interface FKGiftBagListVC ()

@property (nonatomic, weak) UIView *header;
@property (nonatomic, weak) UIImageView *headerImageView;
@property (nonatomic, weak) UIView *footer;
@property (nonatomic, weak) UIImageView *footerImageView;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) NSMutableDictionary *param;
@property (nonatomic, copy) NSString *suffix;
@property(nonatomic, assign) NSInteger page;

@end

@implementation FKGiftBagListVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
        _param[@"pagesize"] = @(10);
    }
    return _param;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"创业礼包";
    
    self.suffix = @"m=Home&c=Item&a=set_meal_list";
    if (self.item_cate_id) {
        self.suffix = @"m=Home&c=Item&a=themeActivityItem";
        self.param[@"item_cate_id"] = self.item_cate_id;
    }
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    UIView *header = [[UIView alloc] init];
    self.tableView.tableHeaderView = header;
    self.header = header;
    
    UIImageView *headerImageView = [[UIImageView alloc] init];
    [header addSubview:headerImageView];
    self.headerImageView = headerImageView;
    [headerImageView fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UIView *footer = [[UIView alloc] init];
    self.tableView.tableFooterView = footer;
    self.footer = footer;
    
    UIImageView *footerImageView = [[UIImageView alloc] init];
    [footer addSubview:footerImageView];
    self.footerImageView = footerImageView;
    [footerImageView fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKBusinessGiftBagCell *cell = [FKBusinessGiftBagCell cellWith:tableView];
    cell.data = self.datas[indexPath.section];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(185);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCommodityDetailVC *vc = [[FKCommodityDetailVC alloc] init];
    NSDictionary *data = self.datas[indexPath.section];
    vc.itemId = data[@"id"];
    vc.package = true;
    fkPushNav(vc);
}

#pragma mark - 网络请求
- (void)loadData
{
    self.page = 1;
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:self.suffix param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            NSDictionary *header = result.data[@"head"];
            NSDictionary *footer = result.data[@"foot"];
            
            fkWeakSelf(self);
            [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:header[@"content"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                CGFloat headerH = fkScreenW * image.size.height / image.size.width;
                weakself.header.frame = CGRectMake(0, 0, fkScreenW, headerH + 10);
                weakself.headerImageView.frame = CGRectMake(12, 10, fkScreenW - 24, headerH);
                weakself.tableView.tableHeaderView = weakself.header;
            }];
            [self.footerImageView sd_setImageWithURL:[NSURL URLWithString:footer[@"content"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                CGFloat footerH = fkScreenW * image.size.height / image.size.width;
                weakself.footer.frame = CGRectMake(0, 0, fkScreenW, footerH + 10);
                weakself.footerImageView.frame = CGRectMake(12, 0, fkScreenW - 24, footerH);
                weakself.tableView.tableFooterView = weakself.footer;
            }];
            
            self.datas = result.data[@"set_meal"];
            [self.tableView reloadData];
            if (self.datas.count > 0) self.page++;
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData
{
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:self.suffix param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_footer endRefreshing];
        if (result.isSuccess) {
            NSArray *newArray = result.data[@"set_meal"];
            if (newArray.count > 0) {
                NSMutableArray *array = [NSMutableArray arrayWithArray:self.datas];
                [array addObjectsFromArray:newArray];
                self.datas = array;
                [self.tableView reloadData];
                self.page++;
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}


@end
